package com.xiaohei.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohei.auth.service.SysUserRoleService;
import com.xiaohei.auth.service.SysUserService;
import com.xiaohei.common.result.Result;
import com.xiaohei.common.utils.MD5;
import com.xiaohei.model.system.SysUser;
import com.xiaohei.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xiaohei
 * @since 2024-04-23
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService service;

    @ApiOperation("用户条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result getPageList(@PathVariable Long page,
                              @PathVariable Long limit,
                              SysUserQueryVo sysUserQueryVo){
        Page<SysUser> pageParam = new Page<>(page, limit);

        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        String username = sysUserQueryVo.getKeyword();
        String beginTime = sysUserQueryVo.getCreateTimeBegin();
        String endTime = sysUserQueryVo.getCreateTimeEnd();
        if(!StringUtils.isEmpty(username)){
            wrapper.like(SysUser::getUsername,username);
        }
        if(!StringUtils.isEmpty(beginTime)){
            wrapper.ge(SysUser::getCreateTime,beginTime);
        }
        if(!StringUtils.isEmpty(endTime)){
            wrapper.le(SysUser::getCreateTime,endTime);
        }

        IPage<SysUser> sysUserPageModel = service.page(pageParam, wrapper);

        return Result.ok(sysUserPageModel);
    }

    @ApiOperation("根据用户id获取用户详情")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable Long id){
        SysUser sysUser = service.getById(id);
        return Result.ok(sysUser);
    }

    @ApiOperation("添加用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser sysUser){
        // 密码加密  使用MD5
        sysUser.setPassword(MD5.encrypt(sysUser.getPassword()));

        boolean is_success = service.save(sysUser);
        if(is_success){
            return Result.ok(is_success);
        }
        return Result.fail();
    }

    @ApiOperation(("删除用户信息"))
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Long id){
        boolean is_success = service.removeById(id);
        if(is_success) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("更新用户信息")
    @PutMapping("update")
    public Result update(@RequestBody SysUser user){
        boolean is_success = service.updateById(user);
        if(is_success){
            return Result.ok(is_success);
        }
        return Result.fail();
    }

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        service.updateStatus(id, status);
        return Result.ok();
    }

}

